package ai.peanut.selfbase.common.utils;

import ai.peanut.selfbase.modules.user.pojo.UserDto;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

public class UserUtils {
    /**
     * 获取当前登录者对象
     */
    public static UserDto getCurrentUser() {
        try {
            Subject subject = SecurityUtils.getSubject();
            UserDto dto = (UserDto) subject.getPrincipal();
            if (dto != null) {
                return dto;
            }
        } catch (UnavailableSecurityManagerException e) {

        } catch (InvalidSessionException e) {

        }
        return null;
    }

    public static Session getSession() {
        try {
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession(false);
            if (session == null) {
                session = subject.getSession();
            }
            if (session != null) {
                return session;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
